package com.risk.management.controller;

import com.risk.management.entity.Risk;
import com.risk.management.service.RiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * 首页控制器
 */
@Controller
public class IndexController {

    @Autowired
    private RiskService riskService;

    /**
     * 系统首页（需要登录）
     */
    @GetMapping("/dashboard")
    public String index(Model model) {
        // 检查用户是否已登录
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null || !auth.isAuthenticated() || "anonymousUser".equals(auth.getPrincipal())) {
            return "redirect:/risk-management/";
        }
        
        // 获取所有风险
        List<Risk> risks = riskService.getAllRisks();
        
        if (risks != null && !risks.isEmpty()) {
            // 计算各种风险统计数据
            int unsolvedCount = (int) risks.stream()
                    .filter(r -> r.getStatus() != 5 && r.getStatus() != 6)
                    .count();
            int solvedCount = (int) risks.stream()
                    .filter(r -> r.getStatus() == 5)
                    .count();
            int highPriorityCount = (int) risks.stream()
                    .filter(r -> r.getLevel() >= 3)
                    .count();
            
            // 添加风险类型统计
            int type1Count = (int) risks.stream().filter(r -> r.getType() == 1).count();
            int type2Count = (int) risks.stream().filter(r -> r.getType() == 2).count();
            int type3Count = (int) risks.stream().filter(r -> r.getType() == 3).count();
            int type4Count = (int) risks.stream().filter(r -> r.getType() == 4).count();
            int type5Count = (int) risks.stream().filter(r -> r.getType() == 5).count();
            
            // 添加风险状态统计
            int status1Count = (int) risks.stream().filter(r -> r.getStatus() == 1).count();
            int status2Count = (int) risks.stream().filter(r -> r.getStatus() == 2).count();
            int status3Count = (int) risks.stream().filter(r -> r.getStatus() == 3).count();
            int status4Count = (int) risks.stream().filter(r -> r.getStatus() == 4).count();
            int status5Count = (int) risks.stream().filter(r -> r.getStatus() == 5).count();
            int status6Count = (int) risks.stream().filter(r -> r.getStatus() == 6).count();
            
            // 添加风险类型统计到模型
            model.addAttribute("type1Count", type1Count);
            model.addAttribute("type2Count", type2Count);
            model.addAttribute("type3Count", type3Count);
            model.addAttribute("type4Count", type4Count);
            model.addAttribute("type5Count", type5Count);
            
            // 添加风险状态统计到模型
            model.addAttribute("status1Count", status1Count);
            model.addAttribute("status2Count", status2Count);
            model.addAttribute("status3Count", status3Count);
            model.addAttribute("status4Count", status4Count);
            model.addAttribute("status5Count", status5Count);
            model.addAttribute("status6Count", status6Count);
            
            // 总数据
            model.addAttribute("totalRisks", risks.size());
            model.addAttribute("openRisks", unsolvedCount);
            model.addAttribute("resolvedRisks", solvedCount);
            model.addAttribute("highPriorityRisks", highPriorityCount);
        } else {
            // 设置默认值
            model.addAttribute("totalRisks", 0);
            model.addAttribute("openRisks", 0);
            model.addAttribute("resolvedRisks", 0);
            model.addAttribute("highPriorityRisks", 0);
            model.addAttribute("type1Count", 0);
            model.addAttribute("type2Count", 0);
            model.addAttribute("type3Count", 0);
            model.addAttribute("type4Count", 0);
            model.addAttribute("type5Count", 0);
            model.addAttribute("status1Count", 0);
            model.addAttribute("status2Count", 0);
            model.addAttribute("status3Count", 0);
            model.addAttribute("status4Count", 0);
            model.addAttribute("status5Count", 0);
            model.addAttribute("status6Count", 0);
        }
        
        return "index";
    }
} 